package cn.kgc.web.service.impl;

import cn.kgc.base.entity.*;
import cn.kgc.base.utils.DateUtil;
import cn.kgc.base.utils.ResponseResult;
import cn.kgc.web.client.CityClient;
import cn.kgc.web.client.ClazzClient;
import cn.kgc.web.client.GroupClient;
import cn.kgc.web.client.TagClient;
import cn.kgc.web.dto.StudentDTO;
import cn.kgc.web.entity.StudentTag;
import cn.kgc.web.mapper.StudentMapper;
import cn.kgc.web.mapper.StudentTagMapper;
import cn.kgc.web.service.StudentService;
import cn.kgc.web.vo.StudentVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author mengshun
* @description 针对表【t_student】的数据库操作Service实现
* @createDate 2024-01-11 09:44:57
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>  implements StudentService{


    @Resource
    private CityClient cityClient;

    @Resource
    private GroupClient groupClient;

    @Resource
    private ClazzClient clazzClient;

    @Resource
    private StudentTagMapper studentTagMapper;

    @Resource
    private TagClient tagClient;


    @Override
    public ResponseResult studentList() {

        //  1. 查询学生表
        List<Student> students = baseMapper.selectList(null);

        List<StudentVO> collect = students.stream().map(student -> {

            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student,studentVO);

            //  2. 根据  cityId  groupId clazzId.....
            Integer cityId = student.getCityid();
            City city= cityClient.getCityById(cityId);
            studentVO.setCity(city);


            Integer groupid = student.getGroupid();
            Group group = groupClient.getGroupById(groupid);
            studentVO.setGroup(group);

            Integer clazzid = student.getClazzid();
            Clazz clazz = clazzClient.getById(clazzid);
            studentVO.setClazz(clazz);

            //  3. 根据学生id  查询学生标签id列表
            Integer sid = student.getId();
            LambdaQueryWrapper<StudentTag> lambda = new QueryWrapper<StudentTag>().lambda();
            lambda.eq(StudentTag::getSid,sid);

            List<Integer> tIds = studentTagMapper.selectList(lambda).stream().map(studentTag -> studentTag.getTid()).collect(Collectors.toList());
            //  4.  根据标签id  查询标签服务
            List<Tag> tags = tIds.stream().map(tid -> {
                Tag tag = tagClient.getTagById(tid);
                return tag;
            }).collect(Collectors.toList());

            studentVO.setTags(tags);

            return studentVO;
        }).collect(Collectors.toList());

        return ResponseResult.success().data(collect);
    }

    @Override
    public ResponseResult addStudent(StudentDTO studentDTO) {

        // 1.  从 StudentDTO  -> student
        Student student = new Student();
        BeanUtils.copyProperties(studentDTO,student);
        Date bir = student.getBir();
        // 年龄
        int age = DateUtil.getAge(bir);
        student.setAge(age);
        // 星座
        String constellation = DateUtil.getConstellation(bir);
        student.setStarts(constellation);
        // 属相
        String year = DateUtil.getYear(bir);
        student.setAttr(year);
        baseMapper.insert(student);

        Integer id = student.getId();

        // 2. StudentDTO  ->  tagIds
        List<Integer> tagIds = studentDTO.getTagIds();

        // 3. 将tagIds  studentId  ->  student_tag
        tagIds.forEach(tid->{
            StudentTag studentTag = new StudentTag();
            studentTag.setTid(tid);
            studentTag.setSid(id);
            studentTagMapper.insert(studentTag);
        });

        // student  -> studentVO
        StudentVO studentVO = new StudentVO();
        BeanUtils.copyProperties(student,studentVO);

        Integer cityId = student.getCityid();
        City city= cityClient.getCityById(cityId);
        studentVO.setCity(city);


        Integer gid = student.getGroupid();
        Group group = groupClient.getGroupById(gid);
        studentVO.setGroup(group);

        Integer clazzId = student.getClazzid();
        Clazz clazz = clazzClient.getById(clazzId);
        studentVO.setClazz(clazz);


        List<Tag> tags = tagIds.stream().map(tid -> {
            Tag tag = tagClient.getTagById(tid);
            return tag;
        }).collect(Collectors.toList());

        studentVO.setTags(tags);
        return ResponseResult.success().data(studentVO).message("添加成功");
    }
}




